package com.ukao.steward.ui.valetRunners.createOrder;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.ukao.steward.R;
import com.ukao.steward.adapter.ClothingTagAdapter;
import com.ukao.steward.adapter.ShoppListAdapter;
import com.ukao.steward.base.BaseFragment;
import com.ukao.steward.bean.ShoppingBean;
import com.ukao.steward.bean.businessWholeBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.L;
import com.ukao.steward.util.T;
import com.ukao.steward.widget.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;

import static com.ukao.steward.consts.Constant.ARG_PARAM10;
import static com.ukao.steward.consts.Constant.ARG_PARAM3;
import static com.ukao.steward.consts.Constant.ARG_PARAM4;
import static com.ukao.steward.consts.Constant.ARG_PARAM5;
import static com.ukao.steward.consts.Constant.ARG_PARAM6;
import static com.ukao.steward.consts.Constant.ARG_PARAM7;
import static com.ukao.steward.consts.Constant.ARG_PARAM8;
import static com.ukao.steward.consts.Constant.ARG_PARAM9;


public class shoppListFragment extends BaseFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.id_tagflowlayout)
    TagFlowLayout idTagflowlayout;
    Unbinder unbinder;
    @BindView(R.id.fra_content)
    FrameLayout fraContent;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private ShoppListAdapter mShoppListAdapter;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    List<SupportFragment> mFragmentData =new ArrayList<>();
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;

    private   ArrayList<businessWholeBean.TypeListBean>  mTabData=new ArrayList<>();
    private int mCurrentPosition=0;
    private ClothingTagAdapter mClothingTagAdapter;
    private   boolean  loadFinish=false;
    private String businessId;
    private String businessName;
    private  ArrayList<ShoppingBean>   mChageData;
    private boolean isValuationEnter;
    private String useId;
    private boolean isStorageEnter;
    private String mercBusinessId;
    private boolean isOrderStockInEnter;
    private String orderId;


    /**
     *
     * @param param1  tab  数据
     * @param businessId   tab  id
     * @param businessName tab  name
     * @param mShoppingDate  修改的数据
     *   @param useId  用户Id
     *    @param isStorageEnter   入库下单进入
     * @return
     */
    public static shoppListFragment newInstance(ArrayList<businessWholeBean.TypeListBean> param1, String businessId,String businessName,
                                                ArrayList<ShoppingBean>    mShoppingDate,
                                                boolean isValuationEnter,String  useId ,
                                                boolean isStorageEnter,String  mercBusinessId,boolean  isOrderStockInEnter,String  orderId  ) {
        shoppListFragment fragment = new shoppListFragment();
        Bundle args = new Bundle();
        args.putParcelableArrayList(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, businessId);
        args.putString(ARG_PARAM3, businessName);
        args.putParcelableArrayList(ARG_PARAM4, mShoppingDate);
        args.putBoolean(ARG_PARAM5, isValuationEnter);
        args.putString(ARG_PARAM6, useId);
        args.putBoolean(ARG_PARAM7, isStorageEnter);
        args.putString(ARG_PARAM8, mercBusinessId);
        args.putBoolean(ARG_PARAM9, isOrderStockInEnter);
        args.putString(ARG_PARAM10, orderId);

        fragment.setArguments(args);
        return fragment;
    }




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mTabData = getArguments().getParcelableArrayList(ARG_PARAM1);
            businessId= getArguments().getString(ARG_PARAM2);/*最上id*/
            businessName= getArguments().getString(ARG_PARAM3);/*最上名称*/
            mChageData= getArguments().getParcelableArrayList(ARG_PARAM4);
            isValuationEnter= getArguments().getBoolean(Constant.ARG_PARAM5,false);
            useId= getArguments().getString(ARG_PARAM6);
            isStorageEnter= getArguments().getBoolean(Constant.ARG_PARAM7,false);
            mercBusinessId= getArguments().getString(ARG_PARAM8);
            isOrderStockInEnter= getArguments().getBoolean(Constant.ARG_PARAM9,false);
            orderId= getArguments().getString(ARG_PARAM10);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_shopp_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        initView();
        initListener();
    }

    @Override
    protected void initView() {
        mClothingTagAdapter= new ClothingTagAdapter(_mActivity, mTabData);
        idTagflowlayout.setAdapter(mClothingTagAdapter);
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if (!loadFinish){
            if (CheckUtils.isNull(mTabData))return;
           // businessWholeBean.TypeListBean  mitem=new businessWholeBean.TypeListBean("全部","",businessId);
          //  mTabData.add(0,mitem);
            mClothingTagAdapter.setSelectedList(mCurrentPosition);
            controlShowFragment(mCurrentPosition);
            loadFinish=true;
        }

    }

    private void controlShowFragment(int position) {
        fragmentManager= getChildFragmentManager();
        fragmentTransaction= fragmentManager.beginTransaction();

        Fragment oldFragment = fragmentManager.findFragmentByTag(makeTag(mCurrentPosition));
        if (oldFragment != null) {
            fragmentTransaction.hide(oldFragment);
        }
        mCurrentPosition = position;

        Fragment currentFragment = fragmentManager.findFragmentByTag(makeTag(position));
        if (currentFragment != null) {
            fragmentTransaction.show(currentFragment);
        } else {

            addFragment(position);
        }
        fragmentTransaction.commitAllowingStateLoss();


    }


    private String makeTag(int position) {
        return R.id.fra_content + "" + position;
    }

    private void addFragment(int position) {
        if (CheckUtils.isEmpty(mTabData))return;
        businessWholeBean.TypeListBean item = mTabData.get(position);
        fragmentTransaction.add(R.id.fra_content, shoppListFragmentChild.newInstance(item.getId(),item.getBusinessId(),
                businessName,mChageData,isValuationEnter,useId,isStorageEnter,mercBusinessId,isOrderStockInEnter,orderId), makeTag(position));

    }


    @Override
    protected void initListener() {
        super.initListener();
        idTagflowlayout.setOnSelectListener(mOnSelectListener);

    }



    private TagFlowLayout.OnSelectListener mOnSelectListener = selectPosSet -> {
        Integer selectpos=0;
        for (Integer pos :
                selectPosSet) {
            selectpos = pos;
        }

        controlShowFragment(selectpos);
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
